package com.github.shiqiyue.app.api.sapi.controller.dbms;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.shiqiyue.app.api.sapi.common.SApiBaseController;
import com.github.shiqiyue.app.api.sapi.controller.dbms.dto.DatabaseDetailDTO;
import com.github.shiqiyue.app.api.sapi.controller.dbms.dto.DatabaseEditReq;
import com.github.shiqiyue.app.api.sapi.controller.dbms.dto.DatabaseListDTO;
import com.github.shiqiyue.app.api.sapi.controller.dbms.dto.DatabasePageDTO;
import com.github.shiqiyue.app.api.sapi.controller.dbms.req.DatabaseAddReq;
import com.github.shiqiyue.app.api.sapi.controller.dbms.req.DatabasePageReq;
import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.common.utils.EnumUtil;
import com.github.shiqiyue.app.modules.dbms.entity.Database;
import com.github.shiqiyue.app.modules.dbms.service.IDatabaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据库 前端控制器
 * </p>
 *
 * @author wenyao.wu
 * @since 2019-08-06
 */
@RestController
@RequestMapping("/sapi/dbms/database")
public class ApiDatabaseController extends SApiBaseController {

    @Autowired
    private IDatabaseService databaseService;

    /***
     * 分页请求
     * @param databasePageReq
     * @return
     */
    @PostMapping("page")
    public Result page(DatabasePageReq databasePageReq) {

        IPage<Database> pageResult = databaseService.page(databasePageReq);
        DatabasePageDTO databasePageDTO = new DatabasePageDTO();
        databasePageDTO.setCurrent(pageResult.getCurrent());
        databasePageDTO.setSize(pageResult.getSize());
        databasePageDTO.setTotal(pageResult.getTotal());
        databasePageDTO.setRecords(pageResult.getRecords().stream().map(DatabasePageDTO.Item::newInstance).collect(Collectors.toList()));

        return Result.success(databasePageDTO);
    }

    /***
     * 列表请求
     * @return
     */
    @PostMapping("list")
    public Result list() {
        List<Database> databaseList = databaseService.list();
        DatabaseListDTO databaseListDTO = new DatabaseListDTO();
        databaseListDTO.setRecords(databaseList.stream().map(DatabaseListDTO.Item::newInstance).collect(Collectors.toList()));
        return Result.success(databaseListDTO);
    }

    /***
     * 详情
     * @param id
     * @return
     */
    @PostMapping("detail")
    public Result detail(Long id) {
        Database database = databaseService.getDatabaseById(id);
        DatabaseDetailDTO databaseDetailDTO = DatabaseDetailDTO.newInstance(database);
        return Result.success(databaseDetailDTO);
    }

    /***
     * 添加
     * @param databaseAddReq
     * @param bindingResult
     * @return
     */
    @PostMapping("add")
    public Result add(@Valid DatabaseAddReq databaseAddReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Database database = new Database();
        database.setName(databaseAddReq.getName());
        database.setUrl(databaseAddReq.getUrl());
        database.setUsername(databaseAddReq.getUsername());
        database.setPassword(databaseAddReq.getPassword());
        database.setRemark(databaseAddReq.getRemark());
        database.setDbType(EnumUtil.getEnumByValue(Database.DbType.values(), databaseAddReq.getDbType()));
        databaseService.add(database, this.getCurrentUser().getId());
        return Result.success();
    }

    /***
     * 修改
     * @param databaseEditReq
     * @param bindingResult
     * @return
     */
    @PostMapping("edit")
    public Result edit(DatabaseEditReq databaseEditReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Database database = new Database();
        database.setId(databaseEditReq.getId());
        database.setName(databaseEditReq.getName());
        database.setUrl(databaseEditReq.getUrl());
        database.setUsername(databaseEditReq.getUsername());
        database.setPassword(databaseEditReq.getPassword());
        database.setRemark(databaseEditReq.getRemark());
        database.setDbType(EnumUtil.getEnumByValue(Database.DbType.values(), databaseEditReq.getDbType()));
        databaseService.edit(database, this.getCurrentUser().getId());
        return Result.success();
    }

}
